package com.yangzb;

import cn.hutool.core.io.IoUtil;
import com.clickhouse.client.*;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

/**
 *
 * @author: yangzb
 * @date 2023/1/3 10:53 AM
 **/
@Slf4j
public class ClickHouseUtil {
    public static void importFile(@NonNull String uris, @NonNull String file, @NonNull String table, ClickHouseFormat format, ClickHouseCompression clickHouseCompression) throws ClickHouseException {
        ClickHouseNodes clickHouseNodes = ClickHouseNodes.of(uris);
        try(ClickHouseClient client = ClickHouseClient.newInstance(ClickHouseProtocol.HTTP)) {
            ClickHouseRequest clickHouseRequest = client.connect(clickHouseNodes);
            ClickHouseRequest.Mutation mutation = clickHouseRequest.write();
            mutation.table(table);
            if(format !=null){
                mutation.format(format);
            }
            if(clickHouseCompression !=null){
                mutation.data(file,clickHouseCompression);
            }else {
                mutation.data(file);
            }
            mutation.executeAndWait();
            IoUtil.closeIfPosible(mutation);
            IoUtil.closeIfPosible(clickHouseRequest);
        }
    }
}
